package com.addcn.pushlibrary.xiaomi;

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

import com.addcn.pushlibrary.LPush;
import com.addcn.pushlibrary.bean.PMessage;
import com.xiaomi.mipush.sdk.MiPushCommandMessage;
import com.xiaomi.mipush.sdk.MiPushMessage;
import com.xiaomi.mipush.sdk.PushMessageReceiver;

import java.util.Map;

import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
import static android.os.PowerManager.SCREEN_DIM_WAKE_LOCK;

/**
 * author:luwenbo
 * created on date : 2019-05-21
 * content:
 * describe:
 */
public class LMiReceive extends PushMessageReceiver {

    //透传
    @Override
    public void onReceivePassThroughMessage(Context context, MiPushMessage miPushMessage) {
        super.onReceivePassThroughMessage(context, miPushMessage);
        Log.i("aa", "onReceivePassThroughMessage:");
        PMessage pMessage = getMessage(miPushMessage);
        PowerManager pm = (PowerManager) context.getSystemService(context.POWER_SERVICE);
        PowerManager.WakeLock mWakelock = pm.newWakeLock(ACQUIRE_CAUSES_WAKEUP | SCREEN_DIM_WAKE_LOCK, "JpushTag:");
        mWakelock.acquire(1000);
        mWakelock.release();
        LPush.getInstance(context).onMsgReceiver(pMessage, true);
    }

    //消息点击
    @Override
    public void onNotificationMessageClicked(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageClicked(context, miPushMessage);
        Log.i("aa", "onNotificationMessageClicked:");
        PMessage pMessage = getMessage(miPushMessage);
        LPush.getInstance(context).onNotificationClicked(pMessage);
    }

    //消息接收
    @Override
    public void onNotificationMessageArrived(Context context, MiPushMessage miPushMessage) {
        super.onNotificationMessageArrived(context, miPushMessage);
        Log.i("aa", "onNotificationMessageArrived:");
        PMessage pMessage = getMessage(miPushMessage);
        LPush.getInstance(context).onMsgReceiver(pMessage,true);
    }

    //用来接受客户端向服务器发送注册命令消息后返回的响应
    @Override
    public void onReceiveRegisterResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        super.onReceiveRegisterResult(context, miPushCommandMessage);
        Log.i("aa", "onReceiveRegisterResult:"+miPushCommandMessage.getResultCode());
    }

    //用来接收客户端向服务器发送命令消息后返回的响应
    @Override
    public void onCommandResult(Context context, MiPushCommandMessage miPushCommandMessage) {
        super.onCommandResult(context, miPushCommandMessage);
        Log.i("aa", "onCommandResult:"+miPushCommandMessage.getResultCode());
    }

    private PMessage getMessage(MiPushMessage miPushMessage) {
        PMessage pMessage = new PMessage();
        Map<String, String> extra = miPushMessage.getExtra();
        if (extra != null) {
            pMessage.setId(extra.get("msg_id") == null ? "" : extra.get("msg_id"));
            pMessage.setUrl(extra.get("url"));
            String clean = "0";
            if (extra.get("clean_notification") != null && !extra.get("clean_notification").equals("")) {
                clean = extra.get("clean_notification");
            }
            pMessage.setClean(clean);
            if (!extra.containsKey("extra")) {
                pMessage.setExtra(extra.get("extra") == null ? "" : extra.get("extra"));
            }
        }

        pMessage.setTitle(miPushMessage.getTitle());
        pMessage.setContent(miPushMessage.getContent());
        //pMessage.setBigThumb(miPushMessage.get);
        pMessage.setFromType(PMessage.FROM_JPUSH);
        return pMessage;
    }
}
